package com.kaili.basic.controller.web.v1;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseProductAddDto;
import com.kaili.basic.pojo.dto.BaseProductQueryDto;
import com.kaili.basic.pojo.entity.BaseProductExport;
import com.kaili.basic.pojo.entity.BaseProductManage;
import com.kaili.basic.service.BaseProductManageService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;


@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "产品管理", description = "产品管理相关API")
public class BaseProductManageController {

    @Autowired
    private BaseProductManageService productManageService;

    @PostMapping("/products")
    @ApiOperation(value = "新增", notes = "新增产品管理数据")
    public RestStatus addBaseProductManage(@Validated @RequestBody BaseProductAddDto productAddDto) {
        if (productAddDto == null) {
            return CommonStatus.BAD_REQUEST;
        }
        productManageService.addProductManage(productAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/products/{productId}")
    @ApiOperation(value = "删除", notes = "删除产品管理数据")
    public RestStatus deleteBaseProductManage(@PathVariable("productId") Long productId) {
        if (productId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        productManageService.deleteProductManage(productId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/products")
    @ApiOperation(value = "更新", notes = "更新产品管理数据")
    public RestStatus updateBaseProductManage(@Validated @RequestBody BaseProductAddDto productAddDto) {
        if (productAddDto != null) {
            productManageService.updateProductManage(productAddDto);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/products")
    @ApiOperation(value = "分页查询产品管理数据", notes = "分页查询事件")
    public Page<BaseProductManage> queryBaseProductManage(@ModelAttribute BaseProductQueryDto productQueryDto) {
        return productManageService.queryProductManage(new Page<BaseProductManage>(productQueryDto.getPage(), productQueryDto.getPageSize()), productQueryDto);
    }

    @GetMapping("/products/like")
    @ApiOperation(value = "根据编号或名称模糊查询产品", notes = "根据编号或名称模糊查询产品")
    public List<BaseProductManage> queryBaseProductManageLike(@ModelAttribute BaseProductQueryDto productQueryDto) {
        return productManageService.queryBaseProductManageLike(productQueryDto);
    }

    @GetMapping("/productCodes")
    @ApiOperation(value = "编号", notes = "查询编号")
    public String getProductManageCode(){
        String code = productManageService.getProductManageCode();
        if (StringUtils.isEmpty(code)){
            return "1";
        }
        return String.valueOf(Integer.valueOf(code)+1);
    }

    @GetMapping("/products/byId/{productId}")
    @ApiOperation(value = "ID", notes = "通过Id查产品")
    public BaseProductManage getProductById(@PathVariable("productId")Long id){
        return productManageService.getProductById(id);
    }

    @GetMapping("/products/export")
    @ApiOperation(value = "产品导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseProductQueryDto queryDto) throws com.kaili.common.exception.RestException {
        List<BaseProductExport> list = productManageService.queryProductManageExport(queryDto);
        ExcelUtil.exportExcel(list,"产品信息","sheet1", BaseProductExport.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/products/import")
    @ApiOperation(value = "产品导入")
    public void export(@RequestBody MultipartFile file) throws RestException {
       List<BaseProductExport> list = ExcelUtil.importExcel(file,1,1, BaseProductExport.class);
       BaseProductAddDto addDto = new BaseProductAddDto();
       for (BaseProductExport productExport : list) {
           addDto.setProductCode(productExport.getProductCode());
           addDto.setProductName(productExport.getProductName());
           addDto.setShipperId(Long.parseLong(productExport.getShipperId()));
           addDto.setRemark(productExport.getRemark());
           addDto.setProductShow(productExport.getProductShow());
           addDto.setAutoCode(productExport.getAutoCode());
           addDto.setCategoryId(Long.parseLong(productExport.getCategoryId()));
           addDto.setFormat(productExport.getFormat());
           addDto.setTrade(StringUtils.isEmpty(productExport.getTrade())?0:Double.parseDouble(productExport.getTrade()));
           addDto.setBuy(StringUtils.isEmpty(productExport.getBuy())?0:Double.parseDouble(productExport.getBuy()));
           addDto.setRetail(StringUtils.isEmpty(productExport.getRetail())?0:Double.parseDouble(productExport.getRetail()));
           addDto.setLength(StringUtils.isEmpty(productExport.getLength())?0:Double.parseDouble(productExport.getLength()));
           addDto.setWidth(StringUtils.isEmpty(productExport.getWidth())?0:Double.parseDouble(productExport.getWidth()));
           addDto.setHeight(StringUtils.isEmpty(productExport.getHeight())?0:Double.parseDouble(productExport.getHeight()));
           addDto.setVolume(StringUtils.isEmpty(productExport.getVolume())?0:Double.parseDouble(productExport.getVolume()));
           addDto.setWeight(StringUtils.isEmpty(productExport.getWeight())?0:Double.parseDouble(productExport.getWeight()));
           addDto.setHelpCode(productExport.getHelpCode());
           addDto.setMeasurementId(Long.parseLong(productExport.getMeasurementId()));
           addDto.setValidity(productExport.getValidity());
           productManageService.addProductManage(addDto);
       }
    }
}
